﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;

namespace ConnectedWebParts.DateAndTime
{
    [ToolboxItemAttribute(false)]
    public class DateAndTime : WebPart
    {
        // attributes to use the OOTB configuration panel - in 
        // this example, we have a custom panel with a drop down list instead
        // of a simple text box for the date format

        //[Personalizable(PersonalizationScope.Shared), 
        //WebBrowsable(true),
        //WebDisplayName("Format de la date"),
        //WebDescription("spéficier le format de la date. Par defaut : dd/MM/yyyy"),
        //Category("Configuration"),
        //DefaultValue("dd/MM/yyy")]

        [WebBrowsable(false), DefaultValue("dd/MM/yyy")]
        public string DateFormat { get; set; }

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        WebDisplayName("display user name"),
        WebDescription("do you want to display the user name"),
        Category("Configuration")]
        public bool DisplayUserName { get; set; }

        protected override void CreateChildControls()
        {
            if(DisplayUserName) this.Controls.Add(new Label { Text = SPContext.Current.Web.CurrentUser.Name });
            this.Controls.Add(new Label { Text = DateTime.Now.ToString(DateFormat) });
        }

        /// <summary>
        /// bind the custom editor with this webpart.
        /// </summary>
        /// <returns></returns>
        public override EditorPartCollection CreateEditorParts()
        {
            EditorPart dateEditor = new DateFormatEditorPart();
            dateEditor.ID = this.ID + "DateFormatPart";
            dateEditor.Title = "Date Format";

            List<EditorPart> editors = new List<EditorPart>();
            editors.Add(dateEditor);

            EditorPartCollection original = base.CreateEditorParts();
            return new EditorPartCollection(original, editors);
        }

    }
}
